/**
 * @ author: 汪孟苹
 */
define(['jquery',"bbcare","comm/form/form",
	"bch/app/patientManager/addPatient.servers",
	"bch/app/patientManager/addPatient.ui"],
	function($,$bb,$form,$servers,$ui){
	var addPatient = {};

	/**
	 * 建档成功后的回调函数
	 */
	var ajaxArchiveCallBack = function(data){
		var params = $bb.getPageParams();
		$bb.toastShow("添加成功！");
		// 跳转
		var arg = {
			"userId":data.id,
			"busiTypeList":params.busiTypeList,
			"subBusiTypeList":params.subBusiTypeList,
		}
		setTimeout(function(){
			if( params.prevRoute ){
				$bb.openPage(params.prevRoute);
			}else{
				$bb.openPage("bch_user",arg);
			}
		},3000);
	}

	/**
	 * 建档保存
	 */
	var onArchiveSave = function(){
		if( $form.validate("archiveBox") ){
			var params = $(this).data("params").params;
			var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			var contList = $form.getSaveData("archiveBox");
			var userName = $bb.code.getValueByName("archiveBox","userName");
			var taskIdList = $servers.getTaskIdList(params.questions);
			var channelId = $bb.getChannelId();
			var params1 = {
				"oprInfo":{
					"opCode":"1000",
					"opType":"2",
					"opNote":"【"+doctorName+"】医生添加【"+userName+"】患者",
					"channelType":"2", // web端门诊渠道:2
					"channelId":channelId,
				},
				"archiveInfo":{
					"queueId":params.queueId,
					"contList":contList,
					"schemeId":params.schemeId,
					"taskIdList":taskIdList,
				},
			};
			$servers.ajaxArchive(params1,ajaxArchiveCallBack);
		}
	}

	/**
	 * 选择不同的临床路径
	 */
	var onSchemeSelect = function(){
		var params = $(this).data("params");
		delete params.questions;
		if( $(this)[0].tagName.toLowerCase() == "input" ){
			var schemeId =  $(this).parents(".radio").find("input:checked").val();
		}else if( $(this)[0].tagName.toLowerCase() == "select" ){
			var schemeId =  $(this).val();
		}
		$.extend(params,{"schemeId":schemeId});
		$servers.ajaxArchiveTaskAndCont(params,ajaxArchiveTaskAndContCallBack);
	}

	/**
	 * 请求队列成功后的回调函数
	 */
	var ajaxArchiveTaskAndContCallBack = function(data,params){
		if( !params.questions ){
			if( data.taskList && data.taskList.length > 0 ){
				params.questions = data.taskList;
			}else if( data.contList && data.contList.length > 0 ){
				params.questions = data.contList;
			}
		}
		$.extend(params,{"queueId":data.queueId,"schemeId":data.schemeId});
		if( params.questions && params.questions.length > 0 ){
			$form.init({
				"id":"archiveBox",
				"questions":params.questions,
				"values":data.valueList,
				"event":{
					"autoCompleteCallBack":ajaxArchiveTaskAndContCallBack,
					"autoCompleteParams":params
				}
			});
			// 选择不同的临床路径
			$("#schemeBox input[name=scheme],#schemeBox select[name=scheme]").unbind("change");
			$("#schemeBox input[name=scheme],#schemeBox select[name=scheme]").data("params",params);
			$("#schemeBox input[name=scheme],#schemeBox select[name=scheme]").change(onSchemeSelect);
			// 保存
			var topbars = [
				{
					"icon":"fa fa-floppy-o",
					"name":"保存",
					"params":{"params":params},
					"callback":onArchiveSave
				}
			];
	        $bb.toolbar(topbars);
		}
	}

	/**
	 * 请求临床路径成功后的回调函数
	 */
	var ajaxQueueCpCallBack = function(data,queueId){
		$ui.archiveWrap({
			"id":"schemeBox",
			"list":data.rows
		});
		var defaultScheme = $servers.getDefaultScheme(data.rows);
		var params = {
			"queueId":queueId,
			"schemeId":defaultScheme.id,
		}
		$servers.ajaxArchiveTaskAndCont(params,ajaxArchiveTaskAndContCallBack);
	}

	/**
	 * 建档页面初始化
	 */
	addPatient.init = function(){
		var params = $bb.getPageParams();
		if( params.title ) $("#addPatientTitle").html(params.title);
		if( params.schemeId ){
			$servers.ajaxArchiveTaskAndCont(params,ajaxArchiveTaskAndContCallBack);
		}else{
			$servers.ajaxQueueCp(params.queueId,ajaxQueueCpCallBack);
		}
	};

	return addPatient;
});